﻿using System;
using System.Collections.Generic;
using System.Text;
using kunukn.RecommendationEngine.AreaRecommendation.Code.Engine;
using kunukn.RecommendationEngine.AreaRecommendation.Code.Entities;
using kunukn.RecommendationEngine.AreaRecommendation.Code.Helpers;
using kunukn.RecommendationEngine.AreaRecommendation.Code.Web;


namespace kunukn.RecommendationEngine.AreaRecommendation.Controls
{
    /// <summary>
    /// Author: Kunuk Nykjaer
    /// </summary>
    public partial class RecommendationView : MyControl
    {
        protected void Page_Init(object sender, EventArgs e)
        {
            if (IsPostBack)
            {
                // cancel item view event because radiobutton or checkbox toggled
                SessionHelper.SetSkipNextTrackingEvent();
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                radioButtons.Items.Add(RecommendationAlgorithm.MaxCount);
                radioButtons.Items.Add(RecommendationAlgorithm.Cosine);
                radioButtons.Items.Add(RecommendationAlgorithm.JaccardIndex);

                radioButtons.SelectedIndex = RecommendationAlgorithm.GetRecommendationModeIndex();
                RecommendationMode.Text = SessionHelper.GetUserSession().RecommendationMode;
                this.checkboxNotSeen.Checked = SessionHelper.GetUserSession().ItemNotSeen;
            }
            else
            {
                SessionHelper.GetUserSession().RecommendationMode = this.radioButtons.SelectedItem.Text;
                SessionHelper.GetUserSession().ItemNotSeen = this.checkboxNotSeen.Checked;
            }
        }
                
        protected void Page_PreRender(object sender, EventArgs e)
        {            
            Item currentItem = base.GetCurrentItem();

            bool itemNotSeen = SessionHelper.GetUserSession().ItemNotSeen;
            List<Item> recommendation = RecommendationAlgorithm.GetRecommendation(currentItem, itemNotSeen);
            this.RecommendationMode.Text = SessionHelper.GetUserSession().RecommendationMode;
            
            if (currentItem == null)
            {
                BestMatchInfo.Text = string.Format("No recommendation found for unknown item");
            }
            
            else if (recommendation.Count == 0)
            {
                BestMatchInfo.Text = string.Format("No recommendation found for item: {0}", currentItem.Name);
            }
            else
            {
                var sb = new StringBuilder();
                foreach (Item item in recommendation)
                {
                    var thumbnail = this.LoadThumbnail(item, true);
                    PanelRecommendedItems.Controls.Add(thumbnail);
                }

                var s = sb.ToString();
                BestMatchInfo.Text = string.Format("You might also be interest in: {0}", s);
            }
        }
    }
}